﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECI.Entities;
using ECI.BaseForm.DynamicUI;
using System.Web.UI.WebControls;
using System.Web.UI;
using ECI.CommonLibrary;

namespace ECI.DynamicData.UIPart
{
    public  class UIPart
    {
        public static void LoadControl(EciTable table, string pageType)
        {
            Page page = PageHelper.CurrentPage;
            int index = 1;
            foreach (EciTableControl tableControl in table.ListControl)
            {
                if (tableControl.ShowMode != pageType && tableControl.ShowMode != "ALL") continue;
                PlaceHolder placeHolder = page.FindControl("PlaceHolder" + index++) as PlaceHolder;
                UserControl myControl = (UserControl)page.LoadControl(tableControl.Path);
                //这里控件分 动态创建的  和 自定义开发的
                // "~/Apps_ECI/Modules/UList.ascx"
                if (tableControl.Type == "DYNAMIC")
                {
                    IDynamicUserControl dynamicUI = (IDynamicUserControl)myControl;

                    IUList uList = (IUList)myControl;
                    if (uList != null)
                    {
                        uList.SetTableControl(tableControl);
                    }
                    dynamicUI.SetTableName(tableControl.ControlCode);
                    int width = 600;
                    int height = 300;
                    if (tableControl.Width.Length > 0) width = tableControl.Width.ToIntIgnoreError();
                    if (tableControl.Height.Length > 0) height = tableControl.Height.ToIntIgnoreError();

                    dynamicUI.SetGvDataParentHeight(height);
                    dynamicUI.SetGvDataParentWidth(width);
                    dynamicUI.SetTitle(tableControl.Title);
                    dynamicUI.SetTitleAlign(tableControl.TitleAlign);
                }

                myControl.ID = tableControl.ClientId;
                if (tableControl.Udf1.Length == 0)
                {
                    if (placeHolder.FindControl(myControl.ID) == null)
                    {
                        placeHolder.Controls.Add(myControl);
                    }
                    else
                    {
                        //已经存在的情况下
                        if (tableControl.Udf5.ToUpper() == "Y")
                        {
                            IDynamicUserControl u = placeHolder.FindControl(myControl.ID) as IDynamicUserControl;
                            u.Reload();
                        }
                    }
                }
                else
                {
                    continue;

                    string displayPosition = tableControl.Udf1;//IN  BEFORE  AFTER
                    string groupName = tableControl.Udf2; //GROUP_CODE

                    LogHelper.Write(tableControl.ClientId + "显示位置=" + displayPosition + " GROUP_NAME=" + groupName, "创建动态控件");

                    #region IN BEFORE AFTER

                    if (displayPosition == "IN")
                    {
                        LogHelper.Write(tableControl.ClientId, "创建动态控件");
                        Table tableParent = page.FindControl("tbEditGroup_" + groupName) as Table;
                        TableRow newRow = new TableRow();
                        TableCell newCell = new TableCell();
                        tableParent.Rows.Add(newRow);
                        newRow.Cells.Add(newCell);
                        newCell.ColumnSpan = tableParent.Rows[0].Cells.Count;
                        newCell.Controls.Add(myControl);

                        LogHelper.Write(tableControl.ClientId + "结束", "创建动态控件");
                    }
                    else if (displayPosition == "BEFORE" || displayPosition == "AFTER")
                    {
                        Table tableRef = page.FindControl("tbEditGroup_" + groupName) as Table;
                        TableRow tableRowRef = tableRef.Parent.Parent as TableRow;

                        Table tableParent = tableRef.Parent.Parent.Parent as Table;
                        TableRow newRow = new TableRow();
                        newRow.ID = "ECI" + tableControl.ClientId;
                        TableCell newCell = new TableCell();
                        int rowIndex = GetRowIndex(tableParent, tableRowRef);
                        if (rowIndex == -1) throw new Exception("配置错误");
                        if (displayPosition == "BEFORE")
                        {
                            tableParent.Rows.AddAt(rowIndex, newRow);
                        }
                        else
                        {
                            tableParent.Rows.AddAt(rowIndex + 1, newRow);
                        }
                        newRow.Cells.Add(newCell);
                        newCell.ColumnSpan = tableParent.Rows[0].Cells.Count;
                        newCell.Controls.Add(myControl);
                    }
                    #endregion
                }
            }
        }

        public static void LoadControl(EciTable table, string groupCode,Table formTableParent,Table formGroupTable)
        {
            string pageType = "SUB";
            Page page = PageHelper.CurrentPage;
            foreach (EciTableControl tableControl in table.ListControl)
            {
                if (tableControl.ShowMode != pageType && tableControl.ShowMode != "ALL") continue;
                string groupName = tableControl.Udf2;
                if (groupCode != groupName) continue;

                UserControl myControl = (UserControl)page.LoadControl(tableControl.Path);
                //这里控件分 动态创建的  和 自定义开发的
                // "~/Apps_ECI/Modules/UList.ascx"
                if (tableControl.Type == "DYNAMIC")
                {
                    IDynamicUserControl dynamicUI = (IDynamicUserControl)myControl;

                    IUList uList = (IUList)myControl;
                    if (uList != null)
                    {
                        uList.SetTableControl(tableControl);
                        uList.SetPostBackLoadUI(true);
                    }
                    dynamicUI.SetTableName(tableControl.ControlCode);
                    int width = 600;
                    int height = 300;
                    if (tableControl.Width.Length > 0) width = tableControl.Width.ToIntIgnoreError();
                    if (tableControl.Height.Length > 0) height = tableControl.Height.ToIntIgnoreError();

                    dynamicUI.SetGvDataParentHeight(height);
                    dynamicUI.SetGvDataParentWidth(width);
                    dynamicUI.SetTitle(tableControl.Title);
                    dynamicUI.SetTitleAlign(tableControl.TitleAlign);
                }

                myControl.ID = tableControl.ClientId;

                string displayPosition = tableControl.Udf1;//IN  BEFORE  AFTER

                LogHelper.Write(tableControl.ClientId + "显示位置=" + displayPosition + " GROUP_NAME=" + groupName, "创建动态控件");

                #region IN BEFORE AFTER

                if (displayPosition == "IN")
                {
                    LogHelper.Write(tableControl.ClientId, "创建动态控件");
                    Table tableParent = formGroupTable;
                    TableRow newRow = new TableRow();
                    TableCell newCell = new TableCell();
                    tableParent.Rows.Add(newRow);
                    newRow.Cells.Add(newCell);
                    newCell.ColumnSpan = tableParent.Rows[0].Cells.Count;
                    newCell.Controls.Add(myControl);

                    LogHelper.Write(tableControl.ClientId + "结束", "创建动态控件");
                }
                else if (displayPosition == "BEFORE" || displayPosition == "AFTER")
                {
                    Table tableRef = formGroupTable;
                    TableRow tableRowRef = tableRef.Parent.Parent as TableRow;

                    Table tableParent = formTableParent;
                    TableRow newRow = new TableRow();
                    newRow.ID = "ECI" + tableControl.ClientId;
                    TableCell newCell = new TableCell();
                    int rowIndex = GetRowIndex(tableParent, tableRowRef);
                    if (rowIndex == -1) throw new Exception("配置错误");
                    if (displayPosition == "BEFORE")
                    {
                        tableParent.Rows.AddAt(rowIndex, newRow);
                    }
                    else
                    {
                        tableParent.Rows.AddAt(rowIndex + 1, newRow);
                    }
                    newRow.Cells.Add(newCell);
                    newCell.ColumnSpan = tableParent.Rows[0].Cells.Count;
                    newCell.Controls.Add(myControl);
                }
                #endregion
            }
        }
 
        public static int GetRowIndex(Table table,TableRow row)
        {
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if (table.Rows[i] == row)
                {
                    return i;
                }
            }

            return -1;
        }
    }
}
